<?php

class Admini_GoodsController extends Uuch_ManageController {
   
    public function init() {
        parent::init();
        $this->appendCss('/admins/css/yundan.css');
        $this->appendCss('/admins/css/common.css');
        $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js');
        $this->setTitle("商品管理");
        $this->view->type = "general";
    }    
    //商品菜单
     public function menuAction()
    {
        $pagesize = 20;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $parentId = $this->getSafeParam("parentId");
        if (!$parentId){
            $parentId = 0;
        }

        $menuAll = Application_Model_Goodsmenu::getFileds($parentId,0,$pagesize,$pageindex);
       
        $this->view->menuAll = $menuAll;
        $this->view->menuList = $menuAll["list"];
        $this->view->parentId = $parentId;
        $this->view->parentItem = Application_Model_Goodsmenu::load($parentId);
        $this->view->stype = 3;
    }
    public function addmenuAction()
    {
        $id = $this->getSafeParam("id");
        $parentId = $this->getSafeParam("parentId");
        $name = $this->getSafeParam("name");
        $isspecial = $this->getSafeParam("isspecial");
        $order = $this->getSafeParam("order");
        $status = $this->getSafeParam("status");        
        $navigationId = Application_Model_Goodsmenu::addmenu($id,$parentId, $name, $order, $status,$isspecial);       
        if($navigationId != ""){
            $this->json(1);
        } else {
            $this->json(0);
        }
    }
      public function menudetailAction()
    {
        $id = $this->getSafeParam("id");
        $navigationItem = Application_Model_Goodsmenu::load($id);
        if($navigationItem != null){
            $this->json(array("sj"=>1,"name"=>$navigationItem->Name,"status"=>$navigationItem->Status,"order"=>$navigationItem->Order,"parentId"=>$navigationItem->parentId));
        } else {
            $this->json(array("sj"=>0));
        }
    }

    public function ordermenuAction()
    {
        $parentId = $this->getSafeParam("parentId");
        $orderAll = $this->getSafeParam("orderAll");
        $arrOder = explode(",", $orderAll);
        $navigationParentItem = Application_Model_Goodsmenu::getAllByParentId($parentId);
        foreach ($navigationParentItem as $key => $npVal) {
            $npVal->Order = $arrOder[$key];
            $npVal->save();
        }
        $this->json(1);
    }


    public function indexAction()
    {
        $pagesize = $this->getSafeParam('pagesize');
        if (!$pagesize) {
            $pagesize = 10;
        }
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $pid = $this->getSafeParam('pid');
        if (!$pid) {
            $pid = 1;
        }
        $smenu=Application_Model_Goodsmenu::getAll(array('parentId'=>$pid,'status'=>1)); 
        $this->setTitle('商品管理');
        $menuid = $this->getSafeParam('menuid');
        if (!$menuid) {
             $menuid = !$pid?1:$pid;
        }       
        $generalizeFilter = Application_Model_Goods::getFilter($menuid,$pagesize,$pageindex); 
        $menu=Application_Model_Goodsmenu::getAll(array('parentId'=>0,'status'=>1,'isspecial'=>0)); 

        $this->view->generalizeList = $generalizeFilter["list"];
        $this->view->generalizeAll = $generalizeFilter;
        $this->view->menu = $menu;
        $this->view->menuid = $menuid;
        $this->view->pid = $pid;
        $this->view->smenu = $smenu;
        $this->view->pagesize = $pagesize;
        $this->view->stype = 4;
    }
    //获取销量
    public function volumeAction()
    {
         $goods = Application_Model_Goods::getAll(array('volume' =>0 )); 
         
         foreach ($goods as $key => $value) {
            $url=$value->url;
         $id=$this->getId($url);
        if($id){ 
        $requestUrl='http://127.0.0.1/tbsdk/TbkItemInfoGetRequest.php?num_iids='.$id;
        $response = file_get_contents($requestUrl);  
        $respObject = @simplexml_load_string($response);
        $array= json_decode(json_encode($respObject),TRUE);       
        $goods1=$array['results']['n_tbk_item'];
        if($goods1['volume']){
            $goods2=Application_Model_Goods::load($value->id);
            $goods2->volume=$goods1['volume'];
            $goods2->save();
        }
        }
        sleep(1);
      }
      print_r(1); exit();
     }

    public function getId($url) {
        $id = '';
        $queryId = 'id';
        $uri = parse_url($url);
        $query = explode('&', $uri['query']);
        $map = array();
        foreach ($query as $key) {
            $str = explode('=', $key);
            if (count($str)>1)
                $map[$str[0]] = $str[1];
            else
                $map[$str[0]] = '';
        }
        return $map[$queryId];
    }

    public function addgoodsAction()
    {
        $keywords = $this->getSafeParam("keywords");
        $menuid = $this->getSafeParam('menuid');
        if($keywords)  $goods=$this->search($keywords);
        else  $goods=[];

        if($this->isPost()){
           $idlist = trim($this->_getParam("goodsAll"),',');
           $menuid = $this->_getParam("menuid");
           $arrid = explode(",", $idlist);
           $bool=true;
           $session_goods=$_SESSION['goods'];
           foreach ($arrid as $key) {            
            if($session_goods[$key]){           
            $addressItem = Application_Model_Goods::add($session_goods[$key],$menuid);
            if(!$addressItem)  $bool=false;
             }
           }          
           if($bool)   $this->json(1);
           else  $this->json(0);
        }
        $this->view->goods = $goods;
        $this->view->menuid =  $menuid;
        $this->view->keywords = $keywords;
        $this->view->stype = 4;
    }

       public function addbannerAction()
    {
        $menuid = $this->getSafeParam("id");
        $goods['item_url'] = $this->getSafeParam("item_url");
        $goods['title'] = $this->getSafeParam("title");
        $goods['pict_url'] = $this->getSafeParam("pict_url");
        $goods['reserve_price']=$this->getSafeParam("color");
        $goods['zk_final_price']=''; 
        $goods['volume']='';     
        $navigationId = Application_Model_Goods::add($goods,$menuid);       
        if($navigationId != ""){
            $this->json(1);
        } else {
            $this->json(0);
        }
    }

    //商品搜索      
    public function search($keywords) {        
        $requestUrl='http://127.0.0.1/tbsdk/TbkItemGetRequest.php?keywords='.$keywords.'&size=100';
        $response = file_get_contents($requestUrl);  
        $respObject = @simplexml_load_string($response);
        $array= json_decode(json_encode($respObject),TRUE);
        $goods=$array['results']['n_tbk_item'];
        $_SESSION['goods']=$goods;
        return $goods;
    }
    //商品详情  
    public function goodsdetailAction() {
        $this->setTitle("商品详情");
        $this->appendCss('/admins/css/adminshare.css');
        $this->appendCss('/admins/css/cangku.css');
        $Id = $this->getSafeParam("id");
        $goodsItem = Application_Model_Goods::load($Id);

        if($this->isPost()){
                $title = $this->getSafeParam("title");
                if($title == ""){
                    $this->message("名称不能为空！");
                    return;
                }
                $url = $this->getSafeParam("url");
                if($url == ""){
                    $this->message("商品地址不能为空！");
                    return;
                }
               $price = $this->getSafeParam("price");
                $cost = $this->getSafeParam("cost");
                $picurl = $this->getSafeParam("picurl");
                $goodsItem->edit($title, $url, $cost, $picurl, $price);
                $this->view->ma = 1;    
        }
        $this->view->Item = $goodsItem;
        $this->view->stype = 4;
    }
    //删除商品
    public function delgoodsAction() {
       $idlist = $this->_getParam("delAll");
       $arrid = explode(",", $idlist);
       for ($i = 0; $i < count($arrid); $i++) {
            $addressItem = Application_Model_Goods::load($arrid[$i]);
            if ($addressItem) {               
                $addressItem->delete();
            }
        }
        $this->json(1);
    }
    /*专题*/
     public function ztindexAction()
    {
        $pagesize = 20;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $parentId = $this->getSafeParam("parentId");
        if (!$parentId){
            $parentId = 0;
        }

        $menuAll = Application_Model_Goodsmenu::getFileds($parentId,1,$pagesize,$pageindex);
       
        $this->view->menuAll = $menuAll;
        $this->view->menuList = $menuAll["list"];
        $this->view->parentId = $parentId;
        $this->view->parentItem = Application_Model_Goodsmenu::load($parentId);
        $this->view->stype = 5;
    }
     public function ztgoodsAction()
    {
        $pagesize = $this->getSafeParam('pagesize');
        if (!$pagesize) {
            $pagesize = 10;
        }
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $pid = $this->getSafeParam('pid');
        if (!$pid) {
            $pid = 0;
            $smenu=[]; 
        }else{
            $smenu=Application_Model_Goodsmenu::getAll(array('parentId'=>$pid,'status'=>1)); 
        }

        $this->setTitle('商品管理');
        $menuid = $this->getSafeParam('menuid');
        if (!$menuid) {
             $menuid = !$pid?1:$pid;
        }       
        $generalizeFilter = Application_Model_Goods::getFilter($menuid,$pagesize,$pageindex); 
        $menu=Application_Model_Goodsmenu::getAll(array('parentId'=>0,'status'=>1,'isspecial'=>1)); 
       
        $this->view->generalizeList = $generalizeFilter["list"];
        $this->view->generalizeAll = $generalizeFilter;
        $this->view->menu = $menu;
        $this->view->menuid = $menuid;
        $this->view->pid = $pid;
        $this->view->smenu = $smenu;
        $this->view->pagesize = $pagesize;
        $this->view->stype = 5;
    }

    //企业云仓库运费
    public function hotindexAction(){
        $type = $this->getSafeParam("type")?$this->getSafeParam("type"):1;
        $where=array();
        if($type)  $where['type']=$type;
        $hot=Application_Model_Cloud::getAll($where);
        $this->view->hotList = $hot;
        $this->view->stype = 2;
    }
    public function addhotAction(){
         if($this->isPost()){
            $item1 = $this->getSafeParam("item1");
            $item2 = $this->getSafeParam("item2");
            $item3 = $this->getSafeParam("item3");
            $item4 = $this->getSafeParam("item4");
            $item5 = $this->getSafeParam("item5");
            $item6 = $this->getSafeParam("item6");
            $type = $this->getSafeParam("type");
            $top = $this->getSafeParam("top");
            $hotId = Application_Model_Cloud::add($item1, $item2, $item3, $item4, $item5,$item6,$type,$top);
            $this->view->ma = 1;
        }
        $this->view->stype = 2;
    }
    public function hotdetailAction() {
        $this->setTitle("企业云仓库运费");
        $hotId = $this->getSafeParam("id");
        $hotItem = Application_Model_Cloud::load($hotId);   
        // print_r($hotItem);    
        if($this->isPost()){          
            $item1 = $this->getSafeParam("item1");
            $item2 = $this->getSafeParam("item2");
            $item3 = $this->getSafeParam("item3");
            $item4 = $this->getSafeParam("item4");
            $item5 = $this->getSafeParam("item5");
            $item6 = $this->getSafeParam("item6");
            $type = $this->getSafeParam("type");
            $top = $this->getSafeParam("top");         
            $hotItem->edithot($item1, $item2, $item3, $item4, $item5,$item6,$type,$top);
            $this->view->ma = 1;    
            } 

        $this->view->hotItem = $hotItem;
        $this->view->stype = 2;
    }
}
